<template>
  <div>
    用户名：<input type="text" v-model="userName">
    <br/>
    <br/>
    密&emsp;码：<input type="password" v-model="password">
    <br/>
    <br/>
    <button @click="handleLogin">登录</button>
  </div>
</template>

<script>
import { mapMutations } from 'vuex'
import qs from 'qs'
export default {
  data() {
    return {
      userName: 'test',
      password: '123456'
    }
  },
  methods: {
    ...mapMutations(['setToken']),
    // handleLogin() {
    //   if (this.userName !== 'admin') return
    //   if (this.password !=='123456') return
    //   this.setToken('1234567890')
    //   this.$router.replace({name: 'index'})
    // }
    handleLogin() {
      let data = qs.stringify({
        username: this.userName,
        password: this.password
      })
      this.$axios({
        url: '/api/u/loginByJson',
        method: 'post',
        data
      }).then(res => {
        let result = res.data.data.accessToken
        this.setToken(result)
        this.$router.replace('/')
      })
    }
  }
}
</script>
